/* global.js */
/* 将输入框的光标移动到指定位置 */
$.fn.selectRange = function(start, end) {
    return this.each(function() {
        if (this.setSelectionRange) {
            this.focus();
            this.setSelectionRange(start, end);
        } else if (this.createTextRange) {
            var range = this.createTextRange();
            range.collapse(true);
            range.moveEnd('character', end);
            range.moveStart('character', start);
            range.select();
        }
    });
};
var MSG = {
    ERR:"系统错误，请刷新重试或联系管理员！"
    , SUCCEED:"操作成功！"
    , LOADING:"内容加载中...请休息一会儿！"
    , AJAX_ERR:"数据加载失败，请刷新重试或联系管理员！"
};
var KEY = {
		UP: 38,
		DOWN: 40,
		DEL: 46,
		TAB: 9,
		RETURN: 13,
		ESC: 27,
		COMMA: 188,
		PAGEUP: 33,
		PAGEDOWN: 34,
		BACKSPACE: 8,
		ENTER: 13		
};
function GetQueryString(name) {
	var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
	var r = window.location.search.substr(1).match(reg);
	if (r != null)
		return unescape(r[2]);
	return null;
}
function Global() {
	var _this = this;
	this.showErrorMsg = function(msg) {
		alert(msg);
	};
	/*
	 * 将焦点放在第一个文本框里
	 */
	this.foucsFirstInput = function() {
		var firstInput = $('.form').find('input[type=text],input[type=password]').filter(':visible:first');
        if (firstInput != null) {
            firstInput.focus();
        }
	};
}
var g = new Global();
//当有输入时自动清除文本框内的默认内容
function defaultKeys(id, gdefaultKey) {
	if(gdefaultKey == undefined || gdefaultKey.length == 0) {
		gdefaultKey = $('#' + id + '_tip').val();
        if(gdefaultKey != undefined && gdefaultKey.length > 0) {
		    $('#' + id).val(gdefaultKey).css('color','#b0b0b0');
        }
	}
	if(gdefaultKey == undefined || gdefaultKey.length == 0) {
		gdefaultKey = $('#' + id).val();
        if(gdefaultKey != undefined && gdefaultKey.length > 0) {
            $('#' + id).val(gdefaultKey).css('color','#b0b0b0');
        }
	}
	return $('#' + id).css('color','#000').click(function(){
		if(gdefaultKey==$(this).val()) {
			$(this).css('color','#b0b0b0').selectRange(0,0);
		}
	}).blur(function(){
		if($(this).val()==undefined||$(this).val().length<=0) {
			$(this).val(gdefaultKey).css('color','#b0b0b0');
		}
	}).keydown(function(event){
		if(gdefaultKey==$(this).val()) {
			$(this).val('').css('color','#000000');
		}
	});
}
//
function AjaxUtils() {
	var _this = this;
	this.submit = function(jqo) {
		var val = jqo.val();
		jqo.val(val + "...").attr('disabled', 'true');
		return true;
	};
	this.unsubmit = function(jqo) {
		var val = jqo.val();
		jqo.val(val.replace('...', '')).removeAttr('disabled');
		return true;
	};
	var loadingId = 'loadingId';
	this.showLoading = function(callback) {
		var div = $('<div><img src="/static/images/loading.gif" alt="" align="absmiddle"/> 努力加载中，请稍候...</div>');
		div.attr('id', loadingId).css({'text-align':'center'
			, 'position':'absolute'
				, 'z-Index':'100'
					, 'background':'#ffff60'
						, '-moz-border-radius':'6px'
							, '-webkit-border-radius':'6px'
								,'padding':'3px 11px'});
		_this.positionCenter(div);
		$('body').prepend(div);
		if(callback) {
			callback();
		}
	};
	this.hideLoading = function(callback) {
		$('#'+loadingId).remove();
		if(callback) {
			callback();
		}
	};
	this.ajaxBgClose = function(callback) {
		$('#ajaxBg').remove();
		if(callback) {
			callback();
		}
	};
	this.ajaxBg = function(callback) {
		var div = $('<div></div>')
			.attr('id', 'ajaxBg')
			.height($(document).height());
		$('body').prepend(div);
		if(callback) {
			callback();
		}
		return div;
	};
	function closePostion(ajaxDiv) {
		var left = ajaxDiv.width()+ajaxDiv.offset().left-50;
		var top = ajaxDiv.offset().top;
		$('#ajaxClose').css({'left':left,'top':top});
	}
	this.ajaxClose = function(ajaxDiv, callback) {
		var close = $('<div><img src="/static/images/close.png" alt="关闭"/></div>').attr('id','ajaxClose');
		$('body').prepend(close);
		closePostion(ajaxDiv);
		if(callback){
			callback();
		}
		return close;
	};
	this.ajaxData = function(callback) {
		var div = $('<div></div>').attr('id', 'ajaxData');
		if(scrollBar=='auto') {
			var _h = offsetDown*2;
			//if(_h<50)h=80;
			var y = $(document).height()-(60);
			//alert(offsetDown*2);
			div.css({'overflow-y':'auto', 'height':y});
		}
		$('body').prepend(div);
		// 让滚动条随屏幕滚动保持在可见位置
		if(callback) {
			callback();
		}
		return div;
	};
	var offsetDown = 80; // 向下
	var offsetRight = 50; // 便宜右侧
	var scrollBar = 'auto';
	this.setOffsetRight = function(r) {
		offsetRight = r;
	};
	this.setOffsetDown = function(d) {
		offsetDown = d;
	};
	this.setScrollBar = function(v) {
		scrollBar = v;
	};
	function setPostion(o) {
		var st = $(document).scrollTop() + offsetDown;
		var sw = $(document).width();
		var dw = $(o).width();
		var l = sw/2 - dw/2 - offsetRight;
		$(o).css({
			top:st
			, left:l
		});
		$('#ajaxBg').height($(document).height());
		closePostion(o);
	};
	this.positionCenter = function(o) {
		setPostion(o);
		resize(o);
	};
	function resize(o) {
		$(window).bind('resize', function() {
			setPostion(o);
		});
		$(o).bind('remove', function(){
			$(window).unbind('resize');
		});
	};
	this.removeAll = function(arr) {
		if(!arr) {
			return ;
		}
		var len = arr.length;
		for(var i=0; i<len; i++) {
			var o = arr[i];
			$(o).remove();
		}
	};
}
var aju = new AjaxUtils();
/**/
$(document).ready(function() {  
	   $("a[rel='external']").click(function(){  
	      this.target = "_blank";  
	   });
	   jQuery.fn.center = function () {  
			this.css("position","absolute");  
			this.css("top", ( $(window).height() - this.height() ) / 2+$(window).scrollTop() + "px");  
			this.css("left", ( $(window).width() - this.width() ) / 2+$(window).scrollLeft() + "px");  
			return this;  
		}  
}); 